<template>
	<view><cropper :imagePath="tempFilePath" :aspectRatio="aspectRatio" @complete="confirm" @cancel="cancel" @spin="spinEvent"></cropper></view>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex';
import { VUE_APP_BASE_API } from '@/common/tools/config.js';
import Cropper from '@/components/yankai-cropper/cropper.vue';

export default {
	data() {
		return {
			tempFilePath: '',
			aspectRatio: null,
			cropFilePath: ''
		};
	},
	components: { Cropper },
	computed: {
		selectedSpec() {
			return getApp().globalData.uploadFileInfo['spec'];
		},
		selectedFile() {
			return getApp().globalData.uploadFileInfo['path'];
		},
		...mapGetters(['token'])
	},
	mounted() {
		this.tempFilePath = this.selectedFile;
		this.aspectRatio = this.selectedSpec.width / this.selectedSpec.height;
		console.log(this.aspectRatio, this.tempFilePath);
	},
	methods: {
		spinEvent() {
			this.aspectRatio = this.aspectRatio >= 1 ? this.selectedSpec.width / this.selectedSpec.height : this.selectedSpec.height / this.selectedSpec.width;
		},
		confirm(file) {
			console.log(file);
			uni.showLoading({ title: '上传中', mask: true });
			uni.uploadFile({
				url: VUE_APP_BASE_API + '/applet/Upload/uploadWechatFile',
				header: {
					'X-Token': this.token,
					'content-type': 'multipart/form-data'
				},
				filePath: file.path,
				formData: {
					upload_type: 2,
					cloud_print_type_id: this.selectedSpec.cloud_print_type_id,
					file_name: ''
				},
				name: 'file',
				success: e => {
					uni.hideLoading();
					console.error(e);
					const data = JSON.parse(e.data);
					if (data.code === 200) {
						//存储文件
						getApp().globalData.uploadFileInfo['path'] = data.result.upload_file;
						getApp().globalData.uploadFileInfo['file_name'] = '图片';
						getApp().globalData.uploadFileInfo['page_num'] = 1;
						//去确认订单
						uni.redirectTo({
							url: '/print-pages/confirm'
						});
					} else {
						// console.error(e)
						uni.showToast({
							title: '服务器繁忙，请稍候再试',
							icon: 'none',
							duration: 5000
						});
					}
				},
				fail: e => {
					console.error(e);
					uni.showToast({
						title: '服务器繁忙，请稍候再试',
						icon: 'none',
						duration: 5000
					});
				}
			});
		},
		cancel() {
			this.tempFilePath = '';
			uni.navigateTo({
				url: '/print-pages/wechat-file-upload'
			});
		}
	}
};
</script>

<style></style>
